package javagame;

import gameobjects.*;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Kraj extends BasicGameState {
	
	public static final int brojSlikaKredits = 36;
	
	private Igrac tenkIgrac;
	private Igrac2 tenkIgrac2;
	private Image slika;
	private Image credits[];
	private Image pressEnt[];
	private Image unlock;
	private Animation credit;
	private Animation pressEnter;
	private int[] duration;
	private int[] duration2;
	private boolean enterStop;
	private boolean fullScreen;
	
	//int mouseX, mouseY;
	
	public Kraj (int state) {}

	public int getID() {
		return 16;
	}

	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		tenkIgrac = Igrac.dohvIgraca();
		tenkIgrac2 = Igrac2.dohvIgraca();
		slika = new Image("res/kraj.png");
		unlock = new Image("res/unlock.png");
		credits = new Image[brojSlikaKredits];
		credits[0] = new Image("res/credits0.png");
		credits[1] = new Image("res/credits1.png");
		credits[2] = new Image("res/credits2.png");
		credits[3] = new Image("res/credits3.png");
		credits[4] = new Image("res/credits4.png");
		credits[5] = new Image("res/credits5.png");
		credits[6] = new Image("res/credits4.png");
		credits[7] = new Image("res/credits3.png");
		credits[8] = new Image("res/credits2.png");
		credits[9] = new Image("res/credits1.png");
		credits[10] = new Image("res/credits0.png");
		credits[11] = new Image("res/credits6.png");
		credits[12] = new Image("res/credits7.png");
		credits[13] = new Image("res/credits8.png");
		credits[14] = new Image("res/credits9.png");
		credits[15] = new Image("res/credits10.png");
		credits[16] = new Image("res/credits9.png");
		credits[17] = new Image("res/credits8.png");
		credits[18] = new Image("res/credits7.png");
		credits[19] = new Image("res/credits6.png");
		credits[20] = new Image("res/credits0.png");
		credits[21] = new Image("res/credits11.png");
		credits[22] = new Image("res/credits12.png");
		credits[23] = new Image("res/credits13.png");
		credits[24] = new Image("res/credits14.png");
		credits[25] = new Image("res/credits15.png");
		credits[26] = new Image("res/credits14.png");
		credits[27] = new Image("res/credits13.png");
		credits[28] = new Image("res/credits12.png");
		credits[29] = new Image("res/credits11.png");
		credits[30] = new Image("res/credits0.png");
		credits[31] = new Image("res/credits16.png");
		credits[32] = new Image("res/credits17.png");
		credits[33] = new Image("res/credits18.png");
		credits[34] = new Image("res/credits19.png");
		credits[35] = new Image("res/credits20.png");
		duration = new int[brojSlikaKredits];
		for(int i=0;i<duration.length;i++) 
			if(i%5 == 0) duration[i] = 2000;
			else duration[i] = 200;
		credit = new Animation(credits, duration, true);
		credit.setLooping(false);
		enterStop = false;
		pressEnt = new Image[2];
		pressEnt[0] = new Image("res/pressEnt.png");
		pressEnt[1] = new Image("res/pressEnt2.png");
		duration2 = new int[2];
		duration2[0] = duration2[1] = 200;
		pressEnter = new Animation(pressEnt, duration2, true);
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.drawImage(slika, 0, 0);
		g.drawAnimation(credit, 237, 327);
		if(credit.getFrame() == 35) {
			g.drawAnimation(pressEnter, 327, 527);
			if (!Nivo.isPresaoIgru())
				g.drawImage(unlock, 221, 598);
		}
	}
	
	public void fullScreenOnOff (GameContainer gc) throws SlickException {
		if (gc.getInput().isKeyDown(Input.KEY_F)) {
			if (fullScreen) {
				gc.setFullscreen(false);
				fullScreen = false;
			}
			else {
				gc.setFullscreen(true);
				fullScreen = true;
			}
		}
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		Input input = gc.getInput();
		if(credit.isStopped() && enterStop==true) {
			credit.restart();
			enterStop=false;
		}
		fullScreenOnOff(gc);
		if(input.isKeyPressed(Input.KEY_ENTER) && credit.getFrame() >= 35) {
			if (!Nivo.isPresaoIgru())
				Nivo.setPresaoIgru(true);
			credit.stop();
			enterStop = true;
			tenkIgrac.ozivi();
			if (Nivo.isDvaIgraca())
				tenkIgrac2.ozivi();
			Nivo.resetLevel();
			Nivo.setNivo(1);
			sbg.enterState(0);
		}
	}

}
